package com.beadhouse.usermanage.web.controller;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.RoleEntity;
import com.beadhouse.entity.UserEntity;
import com.beadhouse.usermanage.ao.UserAo;
import com.beadhouse.usermanage.ao.UserModifyAo;
import com.beadhouse.usermanage.dto.UserModifyDto;
import com.beadhouse.usermanage.service.UserManageService;

@Controller
@Validated
public class UserManageController {
	
	@Autowired
	UserManageService userManageService;
	
	@RequestMapping("/xtsz/gzrygl/list.do")
	public ModelAndView userList(String key,String pageno) throws Exception {
		ModelAndView mav=new ModelAndView("usermanage/gzrygl");
		int pageNo=1;
		if(pageno!=null) {
			pageNo=Integer.parseInt(pageno);
		}
		QueryResult<UserEntity> qr=userManageService.selectUser(key, pageNo);
		List<UserEntity> list=qr.getRows();
		Collections.reverse(list);
		mav.addObject("um",qr);
		
		return mav;
	}
	@RequestMapping("/xtsz/gzrygl/add.do")
	public String userAdd(@Validated UserAo ua,Errors es) throws Exception {
		if(es.hasErrors()) {
			throw new BeadHouseException(es.getAllErrors().get(0).getDefaultMessage());
		}
		userManageService.addUser(ua,1);
		return "forward:/xtsz/gzrygl/list.do"; 
	}
	@RequestMapping("/xtsz/gzrygl/remove.do")
	public String userDelete(@NotBlank String id) throws Exception {
		
		int uid=Integer.parseInt(id);
		Map<String,Object> map=new HashMap<>();
		map.put("isDeleted",true);
		map.put("updateTime",new Date());
		map.put("updateBy",1);
		userManageService.deleteUser(uid, map);
		return "forward:/xtsz/gzrygl/list.do"; 
	}
	@RequestMapping("/xtsz/gzrygl/modify.do")
	public String userModify(@Validated UserModifyAo uma,Errors es) throws NumberFormatException, Exception {
		if(es.hasErrors()) {
			throw new BeadHouseException(es.getAllErrors().get(0).getDefaultMessage());
		}
		Map<String,Object> map=new HashMap<>();
		map.put("sex",Integer.parseInt(uma.getSex()));
		map.put("age",Integer.parseInt(uma.getAge()));
		map.put("updateTime",new Date());
		map.put("updateBy",1);
		map.put("remarks",uma.getRemarks());
		map.put("status","1".equals(uma.getStatus()));
		userManageService.updateUser(Integer.parseInt(uma.getId()), map);
		return "forward:/xtsz/gzrygl/list.do";
	}
	@RequestMapping("/api/xtsz/gzrygl/userCheck")
	@ResponseBody
	public JSONResponse userCheck(@NotBlank String id) throws Exception {
		int uid=Integer.parseInt(id);
		UserModifyDto data=userManageService.selectOne(uid);
		return JSONResponse.ok(data);
	}
}
